package com.zw.system.service.impl;/*
 *@program:java-cloud-prevalence
 *@author:
 *@Time: 2022/12/4  15:41
 *@description: SysRoleServiceImpl业务实现类
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.object.result.Result;
import com.zw.object.system.SysRole;
import com.zw.object.vo.SysRoleQueryVo;
import com.zw.system.mapper.SysRoleMapper;
import com.zw.system.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 查询所有角色业务
     *
     * @return
     */
    @Override
    public Result<List<SysRole>> findAll() {
        return Result.ok(sysRoleMapper.selectList(null));
    }

    /**
     * 逻辑删除业务
     * @param id Role id
     * @return Result
     */
    @Override
    public Result remove(Long id) {
        int success = baseMapper.deleteById(id);
        if(success>0){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 件分页查询业务
     *
     * @param page           当前页
     * @param limit          每页记录数
     * @param sysRoleQueryVo SysRole Vo
     * @return Result
     */
    @Override
    public Result findPageByRole(Long page, Long limit, SysRoleQueryVo sysRoleQueryVo) {
//        封装参数对象
        IPage<SysRole> rolePage = new Page<>(page, limit);
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(sysRoleQueryVo.getRoleName()), SysRole::getRoleName,sysRoleQueryVo.getRoleName())
                        .eq(SysRole::getIsDeleted,0)
                        .orderByDesc(SysRole::getId);

        IPage<SysRole> iPage = baseMapper.selectPage(rolePage, queryWrapper);
        log.info("SysRoleServiceImpl.findPageByRole业务结束，结果:{}",iPage.getRecords());
        return Result.ok(iPage);
    }

    /**
     * 添加角色业务
     *
     * @param sysRole 参数实体类
     * @return Result
     */
    @Override
    public Result saveSysRole(SysRole sysRole) {
        int rows = baseMapper.insert(sysRole);
        if(rows>0){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 根绝id查询业务
     *
     * @param id id
     * @return Result<SysRole>
     */
    @Override
    public Result<SysRole> findSysRoleById(String id) {
        SysRole sysRole = baseMapper.selectById(id);
        if(sysRole == null){
            return Result.fail();
        }
        return Result.ok(sysRole);
    }

    /**
     * 修改Role业务
     *
     * @param sysRole 参数实体类对象
     * @return Result
     */
    @Override
    public Result updateRoleById(SysRole sysRole) {
        int rows = baseMapper.updateById(sysRole);
        if(rows>0){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 根据id批量删除业务
     *
     * @param ids id集合
     * @return Result
     */
    @Override
    public Result batchRemove(List<Long> ids) {
        int rows = baseMapper.deleteBatchIds(ids);
        if(rows>0){
            return Result.ok();
        }
        return Result.fail();
    }
}
